package com.xyhua.houseproperty.controller;

import cn.hutool.core.io.FileUtil;
import com.google.common.io.Files;
import com.google.errorprone.annotations.Var;
import com.xyhua.houseproperty.dto.ResultMsg;
import com.xyhua.houseproperty.entity.User;
import com.xyhua.houseproperty.service.AgentcyService;
import com.xyhua.houseproperty.service.UserService;
import com.xyhua.houseproperty.utils.UserHelper;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * description: UserController <br>
 * date: 2020/3/30 1:39 下午 <br>
 * author: huaxiansheng <br>
 * version: 1.0 <br>
 */
@Controller
public class UserController {

    @Resource
    AgentcyService mAgentcyService;

    @Resource
    UserService mUserService;

    @RequestMapping(value = "accounts/register")
    public String register(User account, ModelMap modelMap, HttpServletRequest httpServletRequest) throws IOException {
        if (account == null || account.getName() == null) {
            modelMap.put("agencyList", mAgentcyService.getAllAgency());
            return "/user/accounts/register";
        }
        ResultMsg resultMsg = UserHelper.validate(account);
        if (resultMsg.isSuccess() && mUserService.addAcount(account)) {
            modelMap.put("email", account.getEmail());
            return "/user/accounts/registerSubmit";
        } else {
            return "redirect:/accounts/register?" + resultMsg.asUrlParams();
        }

    }
}
